package xin.marcher.module.seller.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.seller.domain.dto.SellerAccountDTO;
import xin.marcher.module.seller.domain.request.SellerAccountRequest;
import xin.marcher.module.seller.service.SellerAccountService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 卖家账户管理controller
 */
@RestController
@RequestMapping("/seller/account")
@Slf4j
public class SellerAccountController {

    @Resource
    private SellerAccountService sellerAccountService;

    @PostMapping("/save")
    public JsonResult<Boolean> saveSellerAccountInfo(@RequestBody SellerAccountRequest request) {
        try {
            sellerAccountService.saveSellerAccountInfo(request);
            return JsonResult.buildSuccess();
        } catch (BaseBizException baseBizException) {
            log.error("biz error: request={}", JSON.toJSONString(request), baseBizException);
            return JsonResult.buildError(baseBizException.getMessage());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @PostMapping("/update")
    public JsonResult<Boolean> updateSellerAccountInfo(@RequestBody SellerAccountRequest request) {
        try {
            sellerAccountService.updateSellerAccountInfo(request);
            return JsonResult.buildSuccess();
        } catch (BaseBizException baseBizException) {
            log.error("biz error: request={}", JSON.toJSONString(request), baseBizException);
            return JsonResult.buildError(baseBizException.getMessage());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @GetMapping("/queryAccounts")
    public JsonResult<List<SellerAccountDTO>> queryAccountsBySellerId(Long sellerId) {
        try {
            List<SellerAccountDTO> accountDTOS = sellerAccountService
                    .queryAccountsBySellerId(sellerId);
            return JsonResult.buildSuccess(accountDTOS);
        } catch (BaseBizException baseBizException) {
            log.error("biz error: param={}", JSON.toJSONString(sellerId), baseBizException);
            return JsonResult.buildError(baseBizException.getMessage());
        } catch (Exception e) {
            log.error("system error: param={}", JSON.toJSONString(sellerId), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @PostMapping("/delete")
    public JsonResult<Boolean> deleteAccount(@RequestBody SellerAccountRequest request) {
        try {
            sellerAccountService.deleteAccount(request);
            return JsonResult.buildSuccess();
        } catch (BaseBizException baseBizException) {
            log.error("biz error: request={}", JSON.toJSONString(request), baseBizException);
            return JsonResult.buildError(baseBizException.getMessage());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
